package cz.semjob.learning.relation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cz.semjob.common.attribute.value.NominalStringAttributeValue;
import cz.semjob.document.annotation.RelationWordsPair;
import cz.semjob.document.annotation.metadata.AnnotationTypeFactory;
import cz.semjob.document.processing.DocumentInstance;
import cz.semjob.learning.data.TaxonomyLearningException;
import cz.semjob.learning.data.attribute.NominalInstanceAttribute;
import cz.semjob.learning.instances.UnlabeledRelationInstance;
import cz.semjob.learning.instances.creator.BaseTextInstanceCreator;

@Component
public class NonTaxonomicRelationInstanceCreator extends
		BaseRelationInstanceCreator {

	@Autowired
	private AnnotationTypeFactory annotationTypeFactory;

	/**
	 * 
	 */
	private static final long serialVersionUID = -2524727349900287891L;

	public static final String WORDS1_ENTITY = "Words1Entity";
	public static final String WORDS2_ENTITY = "Words2Entity";

	private NominalInstanceAttribute words1EntityAttribute;

	private NominalInstanceAttribute words2EntityAttribute;

	@Autowired
	public NonTaxonomicRelationInstanceCreator(
			@Qualifier("basetextinstancecreator") BaseTextInstanceCreator textInstanceCreator,
			AnnotationTypeFactory annotationTypeFactory) {
		super(textInstanceCreator, annotationTypeFactory);

		attributesNamesList.add(WORDS1_ENTITY);
		attributesNamesList.add(WORDS2_ENTITY);
	}

	public void initTextInstanceCreator() {
		super.initTextInstanceCreator();

		words1EntityAttribute = new NominalInstanceAttribute(WORDS1_ENTITY,
				annotationTypeFactory.getEntitiesNames());
		words2EntityAttribute = new NominalInstanceAttribute(WORDS2_ENTITY,
				annotationTypeFactory.getEntitiesNames());
		attributesMap.put(WORDS1_ENTITY, words1EntityAttribute);
		attributesMap.put(WORDS2_ENTITY, words2EntityAttribute);
		updateAttributes(attributesNamesList);
	}

	protected void fillRelationInstanceAttributes(
			DocumentInstance documentInstance,
			RelationWordsPair relationWordsPair,
			UnlabeledRelationInstance unlabeledInstance)
			throws TaxonomyLearningException {

		super.fillRelationInstanceAttributes(documentInstance,
				relationWordsPair, unlabeledInstance);
		if (attributesNamesList.contains(WORDS1_ENTITY)) {
			unlabeledInstance.addAttributeValue(WORDS1_ENTITY,
					new NominalStringAttributeValue(WORDS1_ENTITY,
							relationWordsPair.getEntity1().getName(),
							words1EntityAttribute));
		}
		if (attributesNamesList.contains(WORDS2_ENTITY)) {
			unlabeledInstance.addAttributeValue(WORDS2_ENTITY,
					new NominalStringAttributeValue(WORDS1_ENTITY,
							relationWordsPair.getEntity2().getName(),
							words2EntityAttribute));
		}
	}

	public String getName() {
		return "NonTaxonomicRelationInstanceCreator";
	}

	public void setAnnotationTypeFactory(
			AnnotationTypeFactory annotationTypeFactory) {
		this.annotationTypeFactory = annotationTypeFactory;
	}
}
